//
//  XMPositionSelectViewController.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/14.
//

#import "XMPositionSelectViewController.h"
#import "XMPositionSelectView.h"
#import <MapKit/MapKit.h>

@interface XMPositionSelectViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) XMPositionSelectView *positionSelectView;

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@end

@implementation XMPositionSelectViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.locationManager startUpdatingLocation];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.locationManager stopUpdatingLocation];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.locationManager requestWhenInUseAuthorization];
    [self.view addSubview:self.positionSelectView];
}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = [locations firstObject];
    _coordinate = location.coordinate;
    [manager stopUpdatingLocation];
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location
                   completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        [self.positionSelectView setContentWithLocationArr:placemarks];
    }];
//    [geocoder reverseGeocodeLocation:location
//                     preferredLocale:NSLocale.systemLocale
//                   completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
//
//    }];
}

#pragma mark - Init

- (XMPositionSelectView *)positionSelectView {
    if (!_positionSelectView) {
        _positionSelectView = [[XMPositionSelectView alloc] initWithFrame:self.view.bounds];
        XMWeakSelf;
        _positionSelectView.positionSelectBlock = ^(CLPlacemark * _Nonnull placemark, NSString * _Nonnull name) {
            weakSelf.positionSelectBlock ? weakSelf.positionSelectBlock (placemark, name) : nil;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _positionSelectView;
}

- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = 1000000.0f;
        _locationManager.delegate = self;
        [_locationManager requestWhenInUseAuthorization];
    }
    return _locationManager;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
